﻿/*
 * @file: MessageBLL.cs
 * @in namespace: BLL
 */

using System;
using DAL;

/// <summary>
/// Summary description for MessageBLL
/// </summary>
namespace BLL
{
    public class MessageBLL
    {
        public MessageBLL()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region IMessage Members

        /// <summary>
        /// GetMessages
        /// </summary>
        /// <param name="allowFilter">"true" if allow ignore hidden records</param>
        /// <returns>Records corresponding</returns>
        public static Message[] GetMessages(bool allowFilter)
        {
            try
            {
                return DataAccessHelper.GetMessageDA().GetMessages(allowFilter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// GetMessageByOccasionID
        /// </summary>
        /// <param name="occasionID">Occasion ID</param>
        /// <param name="allowFilter">"true" if allow ignore hidden records</param>
        /// <returns>Records corresponding</returns>
        public static Message[] GetMessageByOccasionID(int occasionID, bool allowFilter)
        {
            try
            {
                return DataAccessHelper.GetMessageDA().GetMessageByOccasionID(occasionID, allowFilter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// GetMessageByMessageID
        /// </summary>
        /// <param name="messageID">Message ID</param>
        /// <param name="allowFilter">"true" if allow ignore hidden records</param>
        /// <returns>Record corresponding</returns>
        public static Message GetMessageByMessageID(int messageID, bool allowFilter)
        {
            try
            {
                return DataAccessHelper.GetMessageDA().GetMessageByMessageID(messageID, allowFilter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// InsertMessage
        /// </summary>
        /// <param name="_message">Message Entity</param>
        /// <param name="autoNumber">Current MessageID inserted(output)</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int InsertMessage(Message _message, out int autoNumber)
        {
            try
            {
                if (_message.OccasionID == 0)
                    throw new OnlineFloralDeliveryException(2, "OccasionID is not allow null or less than 0");
                else if (_message.MessageContent == null || _message.MessageContent.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "MessageContent is not allow null");
                else if (MessageDA.ContentExisted(_message.MessageContent) > 0)
                    throw new OnlineFloralDeliveryException(1, "MessageContent already exist");

                return DataAccessHelper.GetMessageDA().InsertMessage(_message, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// DisableMessage
        /// </summary>
        /// <param name="messageID">Message ID</param>
        /// <param name="isEnable">"true" if allow enable Message, "false" if allow hidden Message</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int DisableMessage(int messageID, bool isEnable)
        {
            try
            {
                return DataAccessHelper.GetMessageDA().DisableMessage(messageID, isEnable);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// UpdateMessage
        /// </summary>
        /// <param name="_message">Message entity</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int UpdateMessage(Message _message)
        {
            try
            {
                if (_message.MessageID == 0)
                    throw new OnlineFloralDeliveryException(2, "MessageID is not allow null or less than 0");
                else if (_message.OccasionID == 0)
                    throw new OnlineFloralDeliveryException(2, "OccasionID is not allow null or less than 0");
                else if (_message.MessageContent == null || _message.MessageContent.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "MessageContent is not allow null");
                else if (MessageDA.ContentExisted(_message.MessageContent) > 0)
                    throw new OnlineFloralDeliveryException(1, "MessageContent already exist");

                return DataAccessHelper.GetMessageDA().UpdateMessage(_message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}